#ifndef __VCD_LOG_H__
#define __VCD_LOG_H__
#include <stdio.h>
#include <glib.h>
#include <libgen.h>
#include <string.h>
#include <stdlib.h>
#include "config.h"

G_BEGIN_DECLS

#define LOG_PRIORITY_DEBUG 700  // LOG4C_PRIORITY_DEBUG
#define LOG_PRIORITY_INFO  600  // LOG4C_PRIORITY_INFO
#define LOG_PRIORITY_WARN  400  // LOG4C_PRIORITY_WARN
#define LOG_PRIORITY_ERROR 300  // LOG4C_PRIORITY_ERROR


extern gboolean vcd_log_open_ctl;

void log_message(int priority ,
        const char *file, int line, const char *fun,
        const char *fmt , ...);
void log_message_version_info(int priority ,
        const char *file, int line, const char *fun,
        const char *fmt , ...);


#ifdef __WIN32__
//#define filename(x) strrchr(x,'\\')?strrchr(x,'\\')+1:x
#define filename(x) strrchr(x,'/')?strrchr(x,'/')+1:x
#else
#define filename(x) strrchr(x,'/')?strrchr(x,'/')+1:x
#endif

#define LOG_ERROR(fmt,  args...) log_message(LOG_PRIORITY_ERROR, filename(__FILE__), __LINE__ , __FUNCTION__ , fmt, ##args)
#define LOG_WARN(fmt,   args...) log_message(LOG_PRIORITY_WARN,  filename(__FILE__), __LINE__ , __FUNCTION__ , fmt, ##args)
#define LOG_INFO(fmt,   args...) log_message(LOG_PRIORITY_INFO,  filename(__FILE__), __LINE__ , __FUNCTION__ , fmt, ##args)
#define LOG_DEBUG(fmt , args...) log_message(LOG_PRIORITY_DEBUG, filename(__FILE__), __LINE__ , __FUNCTION__ , fmt, ##args)

#define LOG_VERSION(fmt,  args...) log_message_version_info(LOG_PRIORITY_INFO, filename(__FILE__), __LINE__ , __FUNCTION__ , fmt, ##args)

#define LOG_FPRINTF(fmt,...) fprintf(stderr,fmt,##__VA_ARGS__);fprintf(stderr,"\n")

#ifndef M_DEBUG
#define M_DEBUG LOG_DEBUG
//#define M_DEBUG LOG_FPRINTF
#endif

#ifndef M_INFO
#define M_INFO LOG_INFO
#endif

#ifndef M_ERROR
#define M_ERROR LOG_ERROR
#endif


int vcd_log_init(void);
int vcd_log_init_with_logdir(const char* log4crc,const char* log_dir);
void vcd_log_set_debug_flag(gboolean b);
void vcd_log_set_filename(char* name);
G_END_DECLS

#endif
